define(["Zepto", 
        "underscore", 
        "Backbone", 
        "Handlebars",
        "text!templates/common/updateslistitem.html" 
], function($, _, Backbone, Handlebars, template) {
	
   
var UpdatesListItemView = Backbone.View.extend({

        template: Handlebars.compile(template),

        initialize: function() {
            this.model.bind("change", this.render, this);
            this.model.bind("destroy", this.close, this);
        },

        render: function(eventName) {
            var obj = this.model.toJSON();

            var str = this.model.get('createdAt');
            var iso = str.split('-');
            var date = new Date(parseInt(iso[0], 10), parseInt(iso[1], 10) - 1, parseInt(iso[2], 10));

            var currentDate = new Date();
            if(date.getDay() == currentDate.getDay()
                && date.getMonth() == currentDate.getMonth()
                && date.getFullYear() == currentDate.getFullYear()) {
                obj.data = "Oggi";
            } else if (date.getDay() == currentDate.getDay() - 1
                && date.getMonth() == currentDate.getMonth()
                && date.getFullYear() == currentDate.getFullYear()) {
                obj.data = "Ieri";
            } else if (date.getDay() >= currentDate.getDay() - 6
                && date.getMonth() == currentDate.getMonth()
                && date.getFullYear() == currentDate.getFullYear()) {
                var weekday = ["Dom","Lun","Mar","Mer","Gio","Ven","Sab"];
                obj.data = weekday[date.getDay()];
            } else {
                var month = ["Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre"];
                obj.data = date.getDay() + " " + month[date.getMonth()];
            }

            $(this.el).html(this.template(obj));
            return this;
            //return that;
        }
    });


    return UpdatesListItemView;

});

